package com.cskaoyan.service.admin;

import com.cskaoyan.bean.MarketOrder;
import com.cskaoyan.bean.MarketOrderExample;
import com.cskaoyan.bean.bo.admin.order.MarketOrderSkipBo;
import com.cskaoyan.bean.bo.admin.order.OrderListBO;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.vo.admin.order.MarketOrderDetailVo;
import com.cskaoyan.mapper.MarketOrderMapper;
import com.cskaoyan.utils.DateUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class MarketOrderServiceImpl implements MarketOrderService {

    @Autowired
    MarketOrderMapper marketOrderMapper;

    @Override
    public CommonData<MarketOrder> query(OrderListBO orderListBO, BasePageInfo basePageInfo) {
        PageHelper.startPage(basePageInfo.getPage(), basePageInfo.getLimit());
        MarketOrderExample marketOrderExample = new MarketOrderExample();
        marketOrderExample.setDistinct(true);
        MarketOrderExample.Criteria criteria = marketOrderExample.createCriteria();
        //日期问题待解决
        String s = orderListBO.getStart();
        if (s != null && !"".equals(s)) {
            Date start = new Date();
            try {
                start = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(s);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            criteria.andAddTimeGreaterThanOrEqualTo(start);
        }

        String e = orderListBO.getEnd();
        if (e != null && !"".equals(e)) {
            Date end = new Date();
            try {
                end = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(e);
            } catch (ParseException parseException) {
                parseException.printStackTrace();
            }
            criteria.andEndTimeLessThanOrEqualTo(end);
        }

        Integer userId = orderListBO.getUserId();
        if (userId != null) {
            criteria.andUserIdEqualTo(userId);
        }

        String orderSn = orderListBO.getOrderSn();
        if (orderSn != null && !"".equals(orderSn)) {
            criteria.andOrderSnEqualTo(orderSn);
        }


        criteria.andDeletedEqualTo(false);
        List<Short> orderStatusArray = orderListBO.getOrderStatusArray();
        if (orderStatusArray != null) {
            criteria.andOrderStatusIn(orderStatusArray);
        }

        marketOrderExample.setOrderByClause(basePageInfo.getSort() + " " + basePageInfo.getOrder());
        List<MarketOrder> marketOrders = marketOrderMapper.selectByExample(marketOrderExample);
        PageInfo<MarketOrder> pageInfo = new PageInfo<>(marketOrders);

        return CommonData.data(pageInfo);
    }

    @Override
    public MarketOrderDetailVo detail(String orderId) {
        MarketOrderDetailVo orderDetailVo = new MarketOrderDetailVo();
        // 根据订单查询用户id
        String useId = marketOrderMapper.selectUseIdByOrderId(orderId);
        // 根据订单id查询market_order_goods表
        List<MarketOrderDetailVo.OrderGoods> orderGoods = marketOrderMapper.selectOrderGoodsByOrderId(orderId);
        // 根据商品id查询商品的规格
        for (MarketOrderDetailVo.OrderGoods orderGood : orderGoods) {
            String[] specifications = marketOrderMapper.selectSpecificationsByGoodsId(orderGood.getGoodsId());
            orderGood.setSpecifications(specifications);
        }
        // 根据userId查询market_user表
        MarketOrderDetailVo.User user = marketOrderMapper.selectUserByUserId(useId);
        // 根据订单id查询market_order表
        MarketOrder order = marketOrderMapper.selectOrderById(orderId);
        orderDetailVo.setOrderGoods(orderGoods);
        orderDetailVo.setUser(user);
        orderDetailVo.setOrder(order);
        return orderDetailVo;
    }

    @Override
    public void ship(MarketOrderSkipBo marketOrderSkipBo) {
        MarketOrder marketOrder = new MarketOrder();
        marketOrder.setOrderStatus(401);
        marketOrder.setId(marketOrderSkipBo.getOrderId());
        marketOrder.setShipChannel(marketOrderSkipBo.getShipChannel());
        Date date = DateUtil.getDate();
        marketOrder.setShipTime(date);
        marketOrder.setShipSn(marketOrderSkipBo.getShipSn());
        marketOrderMapper.updateOrdersByShip(marketOrder);
    }

    @Override
    public int delete(String orderId) {
        MarketOrder order = marketOrderMapper.selectOrderById(orderId);
        Integer id = order.getOrderStatus();
        switch (id) {
            case 101:
            case 201:
            case 202:
            case 301:
                return 0;
            default:
                return marketOrderMapper.updateOrderByDelete(orderId);
        }
    }
}
